<?php

class Gliders extends MY_SecuredController {

	function Gliders() {
		parent::MY_SecuredController();

		$this->requireRole('Owner');

		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
		$this->load->library('formdate');
	}

	function index() {
		$this->_preRender(Current_User::user()->id);
		
		$data['gliders'] = GliderModel::findAll();//Doctrine::getTable('GliderModel')->findAll();

		$this->render('admin/gliders/index', $data);
	}

	function see($id) {
		$this->_preRender(Current_User::user()->id);
		
        $data['glider'] = GliderModel::getGlider($id);
        $data['selected'] = $id;
        
        if (!$data['glider']) {
            $this->redirect('admin/gliders/index', array(
                    'message' => 'Ooops, that glider could not be found - choose another one.',
            ), true);        	
        }
        
        $data['gliders'] = GliderModel::findAll();//Doctrine::getTable('GliderModel')->findAll();
        
        $this->render('admin/gliders/view', $data);
    }

	
	function ajax($op, $gliderid) {
		if ($op == 'view') {
			$glider = GliderModel::getGlider($gliderid)->toArray();
				
			echo json_encode($glider);
		}
	}

	function add() {
        $this->_preRender(Current_User::user()->id);
		
        if (!$this->input->post('action')) {
	
			$data['glider'] = new GliderModel();
			$this->render('admin/gliders/add', $data);
		}
		else {
	        if ($this->_submit_validate() === FALSE) {
	            $data['glider'] = new GliderModel();
	            $this->render('admin/gliders/add', $data, $this->error(validation_errors('<li>','</li>')));
	            return;
	        }
	
	        $c = new GliderModel();
	        $c->merge($_POST);
	
	        try {
	            $c->save();
	        }
	        catch (Exception $e) {
	            $this->render('admin/gliders/index', $data, $this->error(array(
	                'message' => 'Ooops, the glider could not be added - please try again later.',
	                'exception' => $e
	            )));
	            return;
	        }
	
	        $this->redirect('admin/gliders/index','Glider added successfully!');
		}
	}

	function edit($id) {
		$this->_preRender(Current_User::user()->id);
		$data['gliders'] = GliderModel::findAll();//Doctrine::getTable('GliderModel')->findAll();
		 
		if (!$this->input->post('action')) {
			$this->_preRender(Current_User::user()->id);

			$data['glider'] = GliderModel::getGlider($id);
			 
			$this->render('admin/gliders/edit', $data);
		}
		else {
			$glider = GliderModel::getGlider($id);

			if (!$glider) {
				$this->render('admin/gliders/index',$data, $this->error(array(
                    'message' => 'Ooops, the glider could not be found, operation has been cancelled!',
				)));
				return;
			}

			$glider->merge($_POST);
			$glider->cert_ltf = $this->input->post('cert_ltf');

			try {
				$glider->save();
			}
			catch (Exception $e) {
				$this->render('admin/gliders/index', $data, $this->error(array(
                    'message' => 'Ooops, the information could not be updated, please try again later.',
                    'exception' => $e
				)));
				return;
			}

			$this->redirect('admin/gliders/index', 'Glider has been successfully updated!');
		}
	}

	function delete($id) {
		$this->_preRender(Current_User::user()->id);
		$data['gliders'] = GliderModel::findAll();//Doctrine::getTable('GliderModel')->findAll();

		$glider = GliderModel::getGlider($id);

		if (!$glider) {
			$this->redirect('admin/gliders/index', array(
                    'message' => 'Ooops, the glider could not be found, operation has been cancelled!',
			));
			return;
		}

		try {
			$glider->delete();
		}
		catch (Exception $e) {
			$this->redirect('admin/gliders/index', array(
                    'message' => 'Ooops, the glider could not be deleted, please try again later.',
                    'exception' => $e
            ), true);
			
			return;
		}

		$this->redirect('admin/gliders/index', 'Glider has been successfully deleted!');
	}

	private function _submit_validate() {
		$this->form_validation->set_rules('brand', 'brand', 'required|alpha_numeric[Glider.brand]');
        $this->form_validation->set_rules('model', 'model', 'required');
		
		return $this->form_validation->run();

	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */